package com.dodock.survey.db;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.dodock.survey.Brave;
import com.dodock.survey.baseclasses.Information;
import com.dodock.survey.baseclasses.Section1;
import com.dodock.survey.baseclasses.Section2;
import com.dodock.survey.baseclasses.Section3;
import com.dodock.survey.baseclasses.Section4;


public class DatabaseManager extends SQLiteOpenHelper {
	
	private static String DB_PATH;
	private static String DB_NAME = "brave.db3";
	private SQLiteDatabase db;
	private final Context context;
	public final int ORDER_RANDOM = -1;
	public final int ORDER_ASCENDING = 1;
	public final int ORDER_DESCENDING = 2;
	public final int ORDER_CURRENT = 0;

	public DatabaseManager(Context context) {
		super(context, DB_NAME, null, 1);
		this.context = context;

		DatabaseManager.DB_PATH = Brave.getFilePath() + "/";
		if (!checkDataBase()) {
			try {
				copyDataBase();
			} catch (IOException e) {}
		}
	}

	private void copyDataBase() throws IOException {
		
		// Open your local db as the input stream
		InputStream myInput = context.getAssets().open(DB_NAME);

		// Path to the just created empty db
		String outFileName = DB_PATH + DB_NAME;

		// Open the empty db as the output stream
		new File(outFileName).createNewFile();
		OutputStream myOutput = new FileOutputStream(outFileName);

		// transfer bytes from the inputfile to the outputfile
		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}
		// Close the streams
		myOutput.flush();
		myOutput.close();
		myInput.close();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	public void closeDB() {
		db.close();
	}

	public void initializeDatabase() {
		try {
			db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,
					SQLiteDatabase.OPEN_READWRITE
							| SQLiteDatabase.NO_LOCALIZED_COLLATORS);
		} catch (Exception ex) {
			ex.printStackTrace();			
		}
	}

	public boolean checkDataBase() {
		String myPath = DB_PATH + DB_NAME;
		if (new File(myPath).exists())
			return true;
		else
			return false;
	}
	
	public void addInformatonIntoDB(ContentValues cv,String key) {		
		if(isInformationExist(key, "information")){
			db.update("information", cv, "key=?", new String[] {  Brave.encryptString(key) } );
		}else{
			db.insert("information", null, cv);
		}		
	}

	public void addSection1DataIntoDB(ContentValues cv,String key) {		
		if(isInformationExist(key,"section1")){
			db.update("section1", cv, "key=?", new String[] {  Brave.encryptString(key) } );
		}else{
			db.insert("section1", null, cv);
		}		
	}
	
	public void addSection2DataIntoDB(ContentValues cv,String key) {		
		if(isInformationExist(key,"section2")){
			db.update("section2", cv, "key=?", new String[] {  Brave.encryptString(key) } );
		}else{
			db.insert("section2", null, cv);
		}		
	}
	
	public void addSection3DataIntoDB(ContentValues cv,String key) {		
		if(isInformationExist(key,"section3")){
			db.update("section3", cv, "key=?", new String[] {  Brave.encryptString(key) } );
		}else{
			db.insert("section3", null, cv);
		}		
	}
	
	public void addSection4DataIntoDB(ContentValues cv,String key) {		
		if(isInformationExist(key,"section4")){
			db.update("section4", cv, "key=?", new String[] {  Brave.encryptString(key) } );
		}else{
			db.insert("section4", null, cv);
		}		
	}

	public boolean isInformationExist(String key, String table) {
		boolean exist;
		Cursor cursor;
		String sql = "SELECT * FROM "+table+" WHERE key='" +  Brave.encryptString(key)
				+ "'";
		cursor = db.rawQuery(sql, null);
		if (cursor.getCount() > 0)
			exist = true;
		else
			exist = false;
		cursor.close();
		return exist;
	}
	
	public Section4 getSection4Data(String key) {
		Section4 section4 = null;
		Cursor cursor;
		String sql = "SELECT * FROM section4 WHERE key=?";
		cursor = db.rawQuery(sql, new String[] {  Brave.encryptString(key) });
		if (cursor.moveToFirst()) {
			section4 = new Section4();			
			section4.setPh1RG(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph1RG"))));
			section4.setPh1year(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph1year"))));
			section4.setPh1value1(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph1value1"))));
			section4.setPh1value2(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph1value2"))));
			section4.setPh1value3(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph1value3"))));
			section4.setPh1value4(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph1value4"))));
			section4.setPh1value5(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph1value5"))));
			section4.setPh2RG(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph2RG"))));
			section4.setPh2year(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph2year"))));
			section4.setPh2value1(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph2value1"))));
			section4.setPh2value2(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph2value2"))));
			section4.setPh2value3(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph2value3"))));
			section4.setPh3RG(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph3RG"))));
			section4.setPh3year(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph3year"))));
			section4.setPh4RG(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph4RG"))));
			section4.setPh4year(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph4year"))));
			section4.setPh5RG(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph5RG"))));
			section4.setPh5year(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph5year"))));
			section4.setPh6RG(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph6RG"))));
			section4.setPh6year(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph6year"))));
			section4.setPh7RG(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph7RG"))));
			section4.setPh7year(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph7year"))));
			section4.setPh8RG(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph8RG"))));
			section4.setPh8year(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph8year"))));
			section4.setPh9RG(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph9RG"))));
			section4.setPh9year(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph9year"))));
			section4.setPh9value(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph9value"))));
			section4.setPh10RG(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph10RG"))));
			section4.setPh10year(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph10year"))));
			section4.setPh10value(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph10value"))));
			section4.setPh11RG(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph11RG"))));
			section4.setPh11year(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph11year"))));
			section4.setPh11value(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph11value"))));
			section4.setPh12value(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph12value"))));			
			section4.setPh13value(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph13value"))));			
			section4.setPh14value(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph14value"))));			
			section4.setPh15value(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("ph15value"))));
		}
		cursor.close();
		return section4;
	}

	public Section3 getSection3Data(String key) {
		Section3 section3 = null;
		Cursor cursor;
		String sql = "SELECT * FROM section3 WHERE key=?";
		cursor = db.rawQuery(sql, new String[] {  Brave.encryptString(key) });
		if (cursor.moveToFirst()) {
			section3 = new Section3();
			section3.setSSO1Date(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO1Date") )));
			section3.setSSO1Time(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO1Time") )));
			section3.setSSO2Date(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO2Date") )));
			section3.setSSO2Time(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO2Time") )));
			section3.setSSO3(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("SSO3"))));
			section3.setSSO4(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("SSO4"))));
			section3.setSSO5(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("SSO5"))));
			section3.setSSO6(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO6"))));
			section3.setSSO7(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("SSO7"))));
			section3.setSSO8(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("SSO8"))));
			section3.setSSO9(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("SSO9"))));
			section3.setSSO10(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("SSO10"))));
			section3.setSSO11(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("SSO11"))));
			section3.setSSO12(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO12"))));
			section3.setSSO13(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("SSO13"))));
			section3.setSSO14(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("SSO14"))));
			section3.setSSO15(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("SSO15"))));
			section3.setSSO16(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("SSO16"))));
			section3.setSSO17(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("SSO17"))));
		}
		cursor.close();
		return section3;
	}
	
	public Section1 getSection1Data(String key) {
		Section1 section1 = null;
		Cursor cursor;
		String sql = "SELECT * FROM section1 WHERE key=?";
		cursor = db.rawQuery(sql, new String[] {  Brave.encryptString(key) });
		if (cursor.moveToFirst()) {
			section1 = new Section1();
			section1.setSystolicBloodPressure1(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("systolicBloodPressure1")) ));			
			section1.setSystolicBloodPressure2(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("systolicBloodPressure2"))));
			section1.setDiastolicBloodPressure1(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("diastolicBloodPressure1"))));
			section1.setDiastolicBloodPressure2(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("diastolicBloodPressure2"))));
			section1.setWrist(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("wrist"))));
			section1.setHip(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("hip"))));
			section1.setWeight(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("weight"))));
			section1.setHeight(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("height"))));
			section1.setHeart_rate(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("heart_rate"))));
		}
		cursor.close();
		return section1;
	}
	
	
	public Section2 getSection2Data(String key) {
		
		Section2 section2 = null;
		Cursor cursor;
		String sql = "SELECT * FROM section2 WHERE key=?";
		cursor = db.rawQuery(sql, new String[] { Brave.encryptString(key) });
		if (cursor.moveToFirst()) {
			section2 = new Section2();			
			section2.setD1(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("d1"))));
			section2.setD2(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("d2"))));
			section2.setD3(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("d3"))));
			section2.setD4(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("d4"))));
			section2.setD5(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("d5"))));
			section2.setD6(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("d6"))));
		}
		cursor.close();
		return section2;
	}

	public Information getInformation(String key) {
		Information information = null;
		Cursor cursor;
		String sql = "SELECT * FROM information WHERE key=?";
		cursor = db.rawQuery(sql, new String[] { Brave.encryptString(key) });
		if (cursor.moveToFirst()) {
			information = new Information();
			information.setInfoId(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoId"))));
			information.setInfoFacilityCode(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoFacilityCode"))));
			information.setInfoSubjectId(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoSubjectId"))));
			information.setInfoSubjectName(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoSubjectName"))));
			information.setInfoAddressAndContactNumbOfSubj(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoAddOfSubj1"))));			
			information.setInfoNameOfHospital(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoNameOfHospital"))));
			information.setInfoWordNoOrOPD(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoWordNoOrOPD"))));
			information.setInfoHospitalRegNo(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoHospitalRegNo"))));
			information.setInfoInterviewerNameOrCode(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("info1stInterviewerNameOrCode"))));
			information.setInfoSecondInterviewerNameOrCode(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("info2ndInterviewerNameOrCode"))));
			information.setInfoUsr1sectionFilled1(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled1"))));
			information.setInfoUsr1sectionFilled2(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled2"))));
			information.setInfoUsr1sectionFilled3(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled3"))));
			information.setInfoUsr1sectionFilled4(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled4"))));
			information.setInfoUsr1sectionFilled5(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled5"))));
			information.setInfoUsr1sectionFilled6(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled6"))));
			information.setInfoUsr1sectionFilled7(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled7"))));
			information.setInfoUsr1sectionFilled8(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled8"))));
			information.setInfoUsr1sectionFilled9(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled9"))));
			information.setInfoUsr1sectionFilled10(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled10"))));
			information.setInfoUsr1sectionFilled11(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled11"))));
			information.setInfoUsr1sectionFilled12(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled12"))));
			information.setInfoUsr1sectionFilled13(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled13"))));
			information.setInfoUsr1sectionFilled14(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled14"))));
			information.setInfoUsr1sectionFilled15(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr1sectionFilled15"))));
			information.setInfoUsr2sectionFilled1(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled1"))));
			information.setInfoUsr2sectionFilled2(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled2"))));
			information.setInfoUsr2sectionFilled3(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled3"))));
			information.setInfoUsr2sectionFilled4(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled4"))));
			information.setInfoUsr2sectionFilled5(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled5"))));
			information.setInfoUsr2sectionFilled6(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled6"))));
			information.setInfoUsr2sectionFilled7(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled7"))));
			information.setInfoUsr2sectionFilled8(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled8"))));
			information.setInfoUsr2sectionFilled9(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled9"))));
			information.setInfoUsr2sectionFilled10(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled10"))));
			information.setInfoUsr2sectionFilled11(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled11"))));
			information.setInfoUsr2sectionFilled12(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled12"))));
			information.setInfoUsr2sectionFilled13(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled13"))));
			information.setInfoUsr2sectionFilled14(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled14"))));
			information.setInfoUsr2sectionFilled15(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled15"))));
			information.setInfoDataCollectionStatus(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoDataCollectionStatus"))));
			information.setInfoReasonForIncomplete(Brave.decryptStringAndReturnInt(cursor.getString(cursor.getColumnIndex("infoReasonForIncomplete"))));
			information.setInfoDateOfEnrolement(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoDateOfEnrolement"))));
			information.setInfoDateOfInterview(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoDateOfInterview"))));
			information.setInfoDateOfSampling(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoDateOfSampling"))));
			information.setInfoDateOfLastMeal(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoDateOfLastMeal"))));
			information.setInfoEndDate(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoEndDate"))));
			information.setInfoEndTime(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoEndTime"))));
			information.setInfoStartTime(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoStartTime"))));
			information.setInfoTimeOfSampling(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoTimeOfSampling"))));
			information.setInfoTimeOfLastMeal(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoTimeOfLastMeal"))));
		}
		cursor.close();
		return information;
	}

	public Vector<Information> getInformationList() {
		Vector<Information> infoList = new Vector<Information>();
		Information information = null;
		Cursor cursor;
		String sql = "SELECT * FROM information";
		cursor = db.rawQuery(sql, null);
		if (cursor.moveToFirst()) {
			do {
				information = new Information();
				information.setKey(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("key"))));
				information.setInfoSubjectName(Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoSubjectName"))));
				infoList.add(information);
			} while (cursor.moveToNext());			
		}
		cursor.close();
		return infoList;
	}

	private String[] columnName = {
			
			"Id","FacilityCode","SubjectId","SubjectName","Address of subject 1","NameOfHospital","WordNoOrOPD","HospitalRegNo",
        	"1stInterviewerNameOrCode","2ndInterviewerNameOrCode","DataCollectionStatus","ReasonForIncomplete","DateOfEnrolement",
        	"DateOfInterview","DateOfSampling","DateOfLastMeal","EndDate","EndTime","StartTime","TimeOfSampling","TimeOfLastMeal",
			
			"systolicBloodPressure1", "systolicBloodPressure2", "diastolicBloodPressure1",
			"diastolicBloodPressure2", "wrist", "hip", "weight", "height", "heart_rate", 
			
			"d1", "d2", "d3", "d4", "d5", "d6", 
			
			"SSO1Date","SSO1Time", "SSO2Date", "SSO2Time", "SSO3", "SSO4", "SSO5", "SSO6",
			"SSO7", "SSO8", "SSO9", "SSO10", "SSO11", "SSO12", "SSO13", "SSO14", "SSO15", "SSO16", "SSO17",
			
			"Ph1 Yes/No", "Ph1 age", "Angina ", "MI", "Angiography", "CABG", "Angioplasty",
			"Ph2 Yes/No", "Ph2 age", "Ischemic", "Hemorrhagic", "Unknown",
			"Ph3 Yes/No", "Ph3 age", "Ph4 Yes/No", "Ph4 age", "Ph5 Yes/No",
			"Ph5 age", "Ph6 Yes/No", "Ph6 age", "Ph7 Yes/No", "Ph7 age",
			"Ph8 Yes/No", "Ph8 age", "Ph9 Yes/No", "Ph9 age", "Ph9 value",
			"Ph10 Yes/No", "Ph10 age", "Ph10 value", "Ph11 Yes/No", "Ph11 age",
			"Ph11 value", "Ph12 value", "Ph13 value", "Ph14 value", "Ph15 value" 
			
	};
	
	
	
	public Boolean exportSection2IntoCsv() {
		String csvHeader = "";
	    String csvValues = "";
	    Boolean returnCode = false;
	    int i = 0;
	    for (i = 0; i < columnName.length; i++) {
	        if (csvHeader.length() > 0) {
	        	csvHeader += ",";
	        }
	        csvHeader +=  columnName[i] ;
	    }

	    csvHeader += "\n";
	    try {
	    	File fileDir = new File(Brave.getExternalSdCardPath()+"Brave/");  
	        fileDir.mkdirs();  
	        File file = new File(fileDir, Brave.getCurrentTimeStamp()+".csv");  
	        FileWriter fileWriter = new FileWriter(file);
	        BufferedWriter out = new BufferedWriter(fileWriter);
	        Cursor cursor;
			String sql = "SELECT * FROM information NATURAL JOIN section1 NATURAL JOIN section2 NATURAL JOIN section3 NATURAL JOIN section4";
			cursor = db.rawQuery(sql, null);
	        if (cursor != null) {
	            out.write(csvHeader);
	            while (cursor.moveToNext()) {
	            	
	            	/*result from information table*/
	            	
	            	
	            	
	            	csvValues  = Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoId"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoFacilityCode"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoSubjectId"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoSubjectName"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoAddOfSubj1"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoNameOfHospital"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoWordNoOrOPD"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoHospitalRegNo"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("info1stInterviewerNameOrCode"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("info2ndInterviewerNameOrCode"))) + ",";
//	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoUsr2sectionFilled15"))) + ",";			
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoDataCollectionStatus"))) + ",";			
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoReasonForIncomplete"))) + ",";			
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoDateOfEnrolement"))) + ",";			
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoDateOfInterview"))) + ",";			
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoDateOfSampling"))) + ",";			
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoDateOfLastMeal"))) + ",";			
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoEndDate"))) + ",";			
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoEndTime"))) + ",";			
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoStartTime"))) + ",";			
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoTimeOfSampling"))) + ",";			
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("infoTimeOfLastMeal"))) + ",";
	            	 

	            	/*result from section1 table*/
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("systolicBloodPressure1"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("systolicBloodPressure2"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("diastolicBloodPressure1"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("diastolicBloodPressure2"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("wrist"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("hip"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("weight"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("height"))) + ",";
	            	csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("heart_rate"))) + ",";
	            	
	            	
	            	/*result from section2 table*/
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("d1"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("d2"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("d3"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("d4"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("d5"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("d6"))) + ",";
	                
	                /*result from section3 table*/
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO1Date"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO1Time"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO2Date"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO2Time"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO3"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO4"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO5"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO6"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO7"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO8"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO9"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO10"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO11"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO12"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO13"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO14"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO15"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO16"))) + ",";
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("SSO17"))) + ",";
	                
	                /*result from section4 table*/
	                csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph1RG"))) + ",";	                
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph1year"))) + ",";	    			
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph1value1"))) + ",";	    			
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph1value2"))) + ",";	    			
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph1value3"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph1value4"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph1value5"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph2RG"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph2year"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph2value1"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph2value2"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph2value3"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph3RG"))) + ",";	    			
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph3year"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph4RG"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph4year"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph5RG"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph5year"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph6RG"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph6year"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph7RG"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph7year"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph8RG"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph8year"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph9RG"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph9year"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph9value"))) + ",";	    			
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph10RG"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph10year"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph10value"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph11RG"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph11year"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph11value"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph12value"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph13value"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph14value"))) + ",";
	    			csvValues += Brave.decryptStringAndReturnString(cursor.getString(cursor.getColumnIndex("ph15value"))) + ",";
	                csvValues +=  "\n";
	                out.write(csvValues);
	            }
	            cursor.close();
	        }
	        out.close();
	        returnCode = true;
	    } catch (IOException e) {
	        returnCode = false;
	    }
	    return returnCode;
	}

}
